package com.gesee.zyspps.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.gesee.zys.R;
import com.gesee.zyspps.db.CmsPoints;

/**
 * 
* @ClassName: MessageAdater
* @Description: TODO(数据通讯)
* @author fujianxiong
* @date 2015年8月29日 上午2:56:47
*
 */
public class MessageAdater extends BaseAdapter
{
    private Context mContext;
    private List<CmsPoints> list;
    private static HashMap<Integer, Boolean> isSelected = new HashMap<Integer, Boolean>();

    public MessageAdater(List<CmsPoints> list,Context ctx)
    {
        this.list = list;
        this.mContext = ctx;
        initDate();
    }
    
    private void initDate()
    {
        if (list == null)
        {
            return;
        }
        for (int i = 0; i < list.size(); i++)
        {
            getIsSelected().put(i, false);
        }
    }

    public void setList(List<CmsPoints> list)
    {
        this.list = list;
        initDate();
    }

    public List<CmsPoints> getList()
    {
        return list;
    }

    public int getCount()
    {
        return list == null ? 0 : list.size();
    }

    public Object getItem(int position)
    {
        return list.get(position);
    }

    public long getItemId(int position)
    {
        return list.get(position).getId();
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        if (convertView == null)
        {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.message_layout, null);
            holder = new ViewHolder();
            holder.tvLeiName = (TextView) convertView.findViewById(R.id.tvLeiName);
            holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        
        CmsPoints point = (CmsPoints) getItem(position);
        
        Map<Integer, Boolean> map = getIsSelected();
        if (map != null && map.size() != 0)
        {
            holder.cb.setChecked(map.get(position));
        }

        holder.tvLeiName.setText(point.getExpNO());
        
        holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1)
            {
                if (arg1)
                {
//                    Set<Entry<Integer, Boolean>> entry = MessageAdater.getIsSelected().entrySet();
//                    
//                    for (Entry<Integer, Boolean> item : entry)
//                    {
//                        if (item.getValue())
//                        {
//                            getIsSelected().put(item.getKey(), false);
//                        }
//                    }
                    getIsSelected().put(position, true);
                }
                else
                {
                    getIsSelected().put(position, false);
                }
                MessageAdater.this.notifyDataSetChanged();
            }
        });

        return convertView;
    }

    public static HashMap<Integer, Boolean> getIsSelected()
    {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected)
    {
        MessageAdater.isSelected = isSelected;
    }
    
    class ViewHolder
    {
        TextView tvLeiName;
        CheckBox cb ;
    }

}
